<?php
if (!(defined('IN_IA'))) 
{
    exit('Access Denied');
}

class Goods_MxShopModel {

    /**
     * ��ȡ��Ʒ���
     * @param type $goodsid
     * @param type $optionid
     * @return type
     */
    public function getOption($goodsid = 0, $optionid = 0) {
        global $_W;
        return pdo_fetch("select * from " . tablename('mx_shop_goods_option') . ' where id=:id and goodsid=:goodsid and uniacid=:uniacid Limit 1', array(':id' => $optionid, ':uniacid' => $_W['uniacid'], ':goodsid' => $goodsid));
    }

    /**
     * ��ȡ��Ʒ���ļ۸�
     * @param type $goodsid
     * @param type $optionid
     * @return type
     */
    public function getOptionPirce($goodsid = 0, $optionid = 0) {
        global $_W;
        return pdo_fetchcolumn("select marketprice from " . tablename('mx_shop_goods_option') . ' where id=:id and goodsid=:goodsid and uniacid=:uniacid', array(':id' => $optionid, ':uniacid' => $_W['uniacid'], ':goodsid' => $goodsid));
    }

    /**
     * ��ȡ����
     * @param type $page
     * @param type $pagesize
     */
    public function getList($args = array()) {

        global $_W;

        $openid = $_W['openid'];
        $page = !empty($args['page']) ? intval($args['page']) : 1;
        $pagesize = !empty($args['pagesize']) ? intval($args['pagesize']) : 10;
        $random = !empty($args['random']) ? $args['random'] : false;

        $displayorder = 'displayorder';
        //ָ���̻�
        $merchid= !empty($args['merchid']) ? trim($args['merchid']) : '';
        if (!empty($merchid)) {
            $displayorder = 'merchdisplayorder';
        }

        $order = !empty($args['order']) ? $args['order'] : ' ' . $displayorder . ' desc,createtime desc';
        $orderby = empty($args['order']) ? '' : (!empty($args['by']) ? $args['by'] : '' );

        //���̻�
        $merch_plugin = p('merch');
        $merch_data = m('common')->getPluginset('merch');
        if ($merch_plugin && $merch_data['is_openmerch']) {
            $is_openmerch = 1;
        } else {
            $is_openmerch = 0;
        }

        $condition = ' and `uniacid` = :uniacid AND `deleted` = 0 and status=1';
        $params = array(':uniacid' => $_W['uniacid']);

        if (!empty($merchid)) {
            $condition.=" and merchid=:merchid and checked=0";
            $params[':merchid'] = $merchid;
        } else {
            if ($is_openmerch == 0) {
                //δ�������̻��������,ֻ��ȡƽ̨��Ʒ
                $condition .= ' and `merchid` = 0';
            } else {
                //�������̻��������,���˵�δͨ����˵���Ʒ
                $condition .= ' and `checked` = 0';
            }
        }

        // ����
        if(empty($args['type'])){
            $condition.=" and type !=10 ";
        }

        //ָ��ID
        $ids = !empty($args['ids']) ? trim($args['ids']) : '';
        if (!empty($ids)) {
            $condition.=" and id in ( " . $ids . ")";
        }

        //��Ʒ
        $isnew = !empty($args['isnew']) ? 1 : 0;
        if (!empty($isnew)) {
            $condition.=" and isnew=1";
        }
        //����
        $ishot = !empty($args['ishot']) ? 1 : 0;
        if (!empty($ishot)) {
            $condition.=" and ishot=1";
        }
        //�Ƽ�
        $isrecommand = !empty($args['isrecommand']) ? 1 : 0;
        if (!empty($isrecommand)) {
            $condition.=" and isrecommand=1";
        }
        //�ۿ�
        $isdiscount = !empty($args['isdiscount']) ? 1 : 0;
        if (!empty($isdiscount)) {
            $condition.=" and isdiscount=1";
        }
        //����
        $issendfree = !empty($args['issendfree']) ? 1 : 0;
        if (!empty($issendfree)) {
            $condition.=" and issendfree=1";
        }

        //��ʱ��
        $istime = !empty($args['istime']) ? 1 : 0;
        if (!empty($istime)) {
            //$condition.=" and istime=1 and " . time() . ">=timestart and " . time() . "<=timeend";
            $condition.=" and istime=1 ";
        }

        //�Ƿ�������
        if (isset($args['nocommission'])) {
            $condition .= ' AND `nocommission`=' . intval($args['nocommission']);
        }

        //�ؼ���
        $keywords = !empty($args['keywords']) ? $args['keywords'] : '';
        if (!empty($keywords)) {
            $condition .= ' AND (`title` LIKE :keywords OR `keywords` LIKE :keywords)';
            $params[':keywords'] = '%' . trim($keywords) . '%';

            if (empty($merchid)) {
                $condition .= ' AND nosearch=0';
            }
        }

        //����
        if(!empty($args['cate'])){
            $category = m('shop')->getAllCategory();
            $catearr = array($args['cate']);
            foreach ($category as $index => $row) {
                if ($row['parentid'] == $args['cate']) {
                    $catearr[] = $row['id'];
                    foreach ($category as $ind => $ro) {
                        if ($ro['parentid'] == $row['id']) {
                            $catearr[] = $ro['id'];
                        }
                    }
                }
            }
            $catearr = array_unique($catearr);
            $condition .= " AND ( ";
            foreach ($catearr as $key=>$value){
                if ($key==0) {
                    $condition .= "FIND_IN_SET({$value},cates)";
                }else{
                    $condition .= " || FIND_IN_SET({$value},cates)";
                }
            }
            $condition .= " <>0 )";
        }

        $member =m('member')->getMember($openid);
        if (!empty($member)) {
            $levelid = intval($member['level']);
            $groupid = intval($member['groupid']);
            $condition.=" and ( ifnull(showlevels,'')='' or FIND_IN_SET( {$levelid},showlevels)<>0 ) ";
            $condition.=" and ( ifnull(showgroups,'')='' or FIND_IN_SET( {$groupid},showgroups)<>0 ) ";
        } else {
            $condition.=" and ifnull(showlevels,'')='' ";
            $condition.=" and   ifnull(showgroups,'')='' ";
        }

        $condition .= " and type <> 99 ";

        $total = "";

        if (!$random) {
            $sql = "SELECT id,title,subtitle,thumb,thumb_url,marketprice,productprice,minprice,maxprice,isdiscount,isdiscount_time,isdiscount_discounts,sales,salesreal,total,description,bargain,`type`,ispresell,`virtual`,hasoption
            FROM " . tablename('mx_shop_goods') . " where 1 {$condition} ORDER BY {$order} {$orderby} LIMIT " . ($page - 1) * $pagesize . ',' . $pagesize;
            $total = pdo_fetchcolumn("select count(*) from " . tablename('mx_shop_goods') . " where 1 {$condition} ",$params);
        } else {
            $sql = "SELECT id,title,thumb,thumb_url,marketprice,productprice,minprice,maxprice,isdiscount,isdiscount_time,isdiscount_discounts,sales,salesreal,total,description,bargain,`type`,ispresell,`virtual`,hasoption
            FROM " . tablename('mx_shop_goods') . " where 1 {$condition} ORDER BY rand() LIMIT " . $pagesize;
            $total  = $pagesize;
        }

        $list = pdo_fetchall($sql, $params);
        foreach($list as $lk => $lv){
            if(p("offic")) {
                //��Ʒ�ڶ�������ͼ
                $list[$lk]['thumb_url'] = array_values(unserialize($lv['thumb_url']));
                $list[$lk]['thumb'] = tomedia($list[$lk]['thumb_url'][0]);
            }
            if($lv['type'] == 3){
                $vsql = 'SELECT * FROM '.tablename('mx_shop_virtual_type').' WHERE uniacid = '.intval($_W['uniacid']).' AND id = '.intval($lv['virtual']);
                $vData = pdo_fetch($vsql);
                if($vData['recycled'] == 1){
                    array_splice($list,$lk,1);
                }
            }
        }
        $list = set_medias($list, 'thumb');
        return array("list"=>$list,"total"=>$total);
    }


    /**
     * ��ȡ����
     * @param type $page
     * @param type $pagesize
     */
    public function getListbyCoupon($args = array()) {

        global $_W;

        $openid = $_W['openid'];
        $page = !empty($args['page']) ? intval($args['page']) : 1;
        $pagesize = !empty($args['pagesize']) ? intval($args['pagesize']) : 10;
        $random = !empty($args['random']) ? $args['random'] : false;

        $order = !empty($args['order']) ? $args['order'] : ' displayorder desc,createtime desc';
        $orderby = empty($args['order']) ? '' : (!empty($args['by']) ? $args['by'] : '' );

        $couponid = empty($args['couponid']) ? '' : $args['couponid'];

        //���̻�
        $merch_plugin = p('merch');
        $merch_data = m('common')->getPluginset('merch');
        if ($merch_plugin && $merch_data['is_openmerch']) {
            $is_openmerch = 1;
        } else {
            $is_openmerch = 0;
        }

        $condition = ' and g.`uniacid` = :uniacid AND g.`deleted` = 0 and g.status=1';
        $params = array(':uniacid' => $_W['uniacid']);

        //ָ���̻�
        $merchid= !empty($args['merchid']) ? trim($args['merchid']) : '';
        if (!empty($merchid)) {
            $condition.=" and g.merchid=:merchid and g.checked=0";
            $params[':merchid'] = $merchid;
        } else {
            if ($is_openmerch == 0) {
                //δ�������̻��������,ֻ��ȡƽ̨��Ʒ
                $condition .= ' and g.`merchid` = 0';
            } else {
                //�������̻��������,���˵�δͨ����˵���Ʒ
                $condition .= ' and g.`checked` = 0';
            }
        }

        // ����
        if(empty($args['type'])){
            $condition.=" and g.type !=10 ";
        }

        //ָ��ID
        $ids = !empty($args['ids']) ? trim($args['ids']) : '';
        if (!empty($ids)) {
            $condition.=" and g.id in ( " . $ids . ")";
        }

        //��Ʒ
        $isnew = !empty($args['isnew']) ? 1 : 0;
        if (!empty($isnew)) {
            $condition.=" and g.isnew=1";
        }
        //����
        $ishot = !empty($args['ishot']) ? 1 : 0;
        if (!empty($ishot)) {
            $condition.=" and g.ishot=1";
        }
        //�Ƽ�
        $isrecommand = !empty($args['isrecommand']) ? 1 : 0;
        if (!empty($isrecommand)) {
            $condition.=" and g.isrecommand=1";
        }
        //�ۿ�
        $isdiscount = !empty($args['isdiscount']) ? 1 : 0;
        if (!empty($isdiscount)) {
            $condition.=" and g.isdiscount=1";
        }
        //����
        $issendfree = !empty($args['issendfree']) ? 1 : 0;
        if (!empty($issendfree)) {
            $condition.=" and g.issendfree=1";
        }

        //��ʱ��
        $istime = !empty($args['istime']) ? 1 : 0;
        if (!empty($istime)) {
            //$condition.=" and istime=1 and " . time() . ">=timestart and " . time() . "<=timeend";
            $condition.=" and g.istime=1 ";
        }

        //�Ƿ�������
        if (isset($args['nocommission'])) {
            $condition .= ' AND g.`nocommission`=' . intval($args['nocommission']);
        }

        //�ؼ���
        $keywords = !empty($args['keywords']) ? $args['keywords'] : '';
        if (!empty($keywords)) {
            $condition .= ' AND (g.`title` LIKE :keywords OR g.`keywords` LIKE :keywords)';
            $params[':keywords'] = '%' . trim($keywords) . '%';
        }

        //����
        if(!empty($args['cate'])){
            $category = m('shop')->getAllCategory();
            $catearr = array($args['cate']);
            foreach ($category as $index => $row) {
                if ($row['parentid'] == $args['cate']) {
                    $catearr[] = $row['id'];
                    foreach ($category as $ind => $ro) {
                        if ($ro['parentid'] == $row['id']) {
                            $catearr[] = $ro['id'];
                        }
                    }
                }
            }
            $catearr = array_unique($catearr);
            $condition .= " AND ( ";
            foreach ($catearr as $key=>$value){
                if ($key==0) {
                    $condition .= "FIND_IN_SET({$value},g.cates)";
                }else{
                    $condition .= " || FIND_IN_SET({$value},g.cates)";
                }
            }
            $condition .= " <>0 )";
        }

        $member =m('member')->getMember($openid);
        if (!empty($member)) {
            $levelid = intval($member['level']);
            $groupid = intval($member['groupid']);
            $condition.=" and ( ifnull(g.showlevels,'')='' or FIND_IN_SET( {$levelid},g.showlevels)<>0 ) ";
            $condition.=" and ( ifnull(g.showgroups,'')='' or FIND_IN_SET( {$groupid},g.showgroups)<>0 ) ";
        } else {
            $condition.=" and ifnull(g.showlevels,'')='' ";
            $condition.=" and   ifnull(g.showgroups,'')='' ";
        }


        $table =tablename('mx_shop_goods').' g';

        if($couponid>0)
        {
            $data = pdo_fetch('select c.*  from ' . tablename('mx_shop_coupon_data') . '  cd inner join  ' . tablename('mx_shop_coupon') . ' c on cd.couponid = c.id  where cd.id=:id and cd.uniacid=:uniacid and coupontype =0  limit 1', array(':id' => $couponid, ':uniacid' => $_W['uniacid']));
            if(!empty($data))
            {
                $i=0;
                $condition2="";

                if($data['limitgoodcatetype']==1&&!empty($data['limitgoodcateids']))
                {
                    $limitcateids=explode(',',$data['limitgoodcateids']);
                    if(count($limitcateids)>0)
                    {
                        foreach($limitcateids as $cateid)
                        {
                            $i++;
                            if($i>1)
                            {
                                $condition2 .=' or ';
                            }
                            $condition2 .=' FIND_IN_SET('.$cateid.',g.cates)';
                        }
                    }
                }

                if($data['limitgoodtype']==1&&!empty($data['limitgoodids']))
                {
                    $i++;
                    if($i>1)
                    {
                        $condition2 .=' or ';
                    }

                    $condition2 .='  g.id in ('.$data['limitgoodids'].') ';
                }
                if(!empty($condition2))
                {
                    $condition .='and ('.$condition2.') ';
                }                
            }
        }

        if (!$random) {
            $sql = "SELECT  g.*  FROM " .$table . " where 1 {$condition} ORDER BY {$order} {$orderby} LIMIT " . ($page - 1) * $pagesize . ',' . $pagesize;
            $total = pdo_fetchcolumn("select  count(*) from " . $table . " where 1 {$condition} ",$params);
        } else {
            $sql = "SELECT  g.*  FROM " . $table . " where 1 {$condition} ORDER BY rand() LIMIT " . $pagesize;
            $total  = $pagesize;
        }

        $list = pdo_fetchall($sql, $params);
        $list = set_medias($list, 'thumb');
        return array("list"=>$list,"total"=>$total);
    }

    public function getTotals() {
        global $_W;
        return array(
            "sale" => pdo_fetchcolumn('select count(1) from ' . tablename('mx_shop_goods') . " where status > 0 and checked=0 and deleted=0 and total>0 and  type !=30 and uniacid=:uniacid", array(':uniacid' => $_W['uniacid'])),
            "out" => pdo_fetchcolumn('select count(1) from ' . tablename('mx_shop_goods') . " where status > 0 and deleted=0 and total=0 and type !=30 and uniacid=:uniacid", array(':uniacid' => $_W['uniacid'])),
            "stock" => pdo_fetchcolumn('select count(1) from ' . tablename('mx_shop_goods') . " where (status=0 or checked=1) and deleted=0 and uniacid=:uniacid and type !=30", array(':uniacid' => $_W['uniacid'])),
            "cycle" => pdo_fetchcolumn('select count(1) from ' . tablename('mx_shop_goods') . " where deleted=1 and uniacid=:uniacid and type !=30", array(':uniacid' => $_W['uniacid'])),
            "verify" => pdo_fetchcolumn('select count(1) from ' . tablename('mx_shop_goods') . " where deleted=0  and uniacid=:uniacid and type !=30 and merchid>0 and checked=1", array(':uniacid' => $_W['uniacid'])),
        );
    }

    /**
     * ��ȡ��������
     * @param type $page
     * @param type $pagesize
     */
    public function getComments($goodsid = '0', $args = array()) {

        global $_W;
        $page = !empty($args['page']) ? intval($args['page']) : 1;
        $pagesize = !empty($args['pagesize']) ? intval($args['pagesize']) : 10;

        $condition = ' and `uniacid` = :uniacid AND `goodsid` = :goodsid and deleted=0';
        $params = array(':uniacid' => $_W['uniacid'], ':goodsid' => $goodsid);
        $sql = "SELECT id,nickname,headimgurl,content,images FROM " . tablename('mx_shop_goods_comment') . " where 1 {$condition} ORDER BY createtime desc LIMIT " . ($page - 1) * $pagesize . ',' . $pagesize;
        $list = pdo_fetchall($sql, $params);
        foreach ($list as &$row) {
            $row['images'] = set_medias(unserialize($row['images']));
        }
        unset($row);
        return $list;
    }

    public function isFavorite($id = '') {

        global $_W;

        $count = pdo_fetchcolumn('select count(*) from ' . tablename('mx_shop_member_favorite') . " where goodsid=:goodsid and deleted=0 and openid=:openid and uniacid=:uniacid limit 1", array(':goodsid' => $id, ':openid' => $_W['openid'], ':uniacid' => $_W['uniacid']));
        return $count > 0;
    }

    public function addHistory($goodsid = 0) {
        global $_W;

        //�������
        pdo_query("update " . tablename('mx_shop_goods') . " set viewcount=viewcount+1 where id=:id and uniacid='{$_W[uniacid]}' ", array(":id" => $goodsid));

        //�����¼
        $history = pdo_fetch('select id,times from ' . tablename('mx_shop_member_history') . ' where goodsid=:goodsid and uniacid=:uniacid and openid=:openid limit 1'
            , array(':goodsid' => $goodsid, ':uniacid' => $_W['uniacid'], ':openid' => $_W['openid']));
        if (empty($history)) {
            $history = array(
                'uniacid' => $_W['uniacid'],
                'openid' => $_W['openid'],
                'goodsid' => $goodsid,
                'deleted' => 0,
                'createtime' => time(),
                'times' => 1
            );
            pdo_insert('mx_shop_member_history', $history);
        } else {
            pdo_update('mx_shop_member_history', array('deleted' => 0, 'times' => $history['times'] + 1), array('id' => $history['id']));
        }
    }

    public function getCartCount($isnewstore = 0) {

        global $_W, $_GPC;
//		if (empty($_W['mid'])) {
//
//			$carts = m('cookie')->get('carts');
//			if (!is_array($carts)) {
//				return 0;
//			}
//
//			$cartcount = 0;
//			foreach ($carts as $c => $total) {
//				$cartcount+=$total;
//			}
//			return $cartcount;
//		}
        $paras  = array(':uniacid' =>  $_W['uniacid']);
        $paras[':openid'] =$_W['openid'];

//        �����O2O�Ĺ��ﳵ
        $sqlcondition="";
        if($isnewstore !=0){
            $sqlcondition=" and isnewstore=:isnewstore";
            $paras[':isnewstore'] =$isnewstore;
        }

        $count = pdo_fetchcolumn('select sum(total) from ' . tablename('mx_shop_member_cart') . " where uniacid=:uniacid and openid=:openid ".$sqlcondition." and deleted=0 limit 1", $paras);
        return $count;
    }

    /**
     * ��ȡ��Ʒ���ͼƬ
     * @param type $specs
     * @return type
     */
    public function getSpecThumb($specs) {
        global $_W;

        $thumb = '';
        $cartspecs = explode('_', $specs);
        $specid = $cartspecs[0];

        if (!empty($specid)) {
            $spec = pdo_fetch("select thumb from " . tablename('mx_shop_goods_spec_item') . " "
                . " where id=:id and uniacid=:uniacid limit 1 ", array(':id' => $specid, ':uniacid' => $_W['uniacid']));

            if (!empty($spec)) {
                if (!empty($spec['thumb'])) {
                    $thumb = $spec['thumb'];
                }
            }
        }
        return $thumb;
    }

    /**
     * ��ȡ��Ʒ���ͼƬ
     * @param type $specs
     * @return type
     */
    public function getOptionThumb($goodsid = 0, $optionid = 0) {
        global $_W;

        $thumb = '';
        $option  = $this->getOption($goodsid, $optionid);
        if (!empty($option)) {
            $specs = $option['specs'];
            $thumb = $this->getSpecThumb($specs);
        }
        return $thumb;
    }

    public function getAllMinPrice($goods)
    {
        global $_W;

        if (is_array($goods))
        {
            $openid =$_W['openid'];
            $level = m('member')->getLevel($openid);
            $member = m('member')->getMember($openid);
            $levelid = $member['level'];

            foreach ($goods as &$value){
                $minprice = $value['minprice']; $maxprice = $value['maxprice'] ;

                if($value['isdiscount'] && $value['isdiscount_time']>=time()){
                    $value['oldmaxprice'] = $maxprice;
                    $isdiscount_discounts = json_decode($value['isdiscount_discounts'],true);
                    $prices = array();

                    if (!isset($isdiscount_discounts['type']) || empty($isdiscount_discounts['type'])) {
                        //ͳһ����
                        $prices_array = m('order')->getGoodsDiscountPrice($value, $level, 1);
                        $prices[] = $prices_array['price'];
                    } else {
                        //��ϸ����
                        $goods_discounts = m('order')->getGoodsDiscounts($value, $isdiscount_discounts, $levelid);
                        $prices = $goods_discounts['prices'];
                    }
                    $minprice = min($prices);
                    $maxprice = max($prices);
                }

                $value['minprice'] = $minprice;
                $value['maxprice'] = $maxprice;
            }
            unset($value);
        }
        else
        {
            $goods = array();
        }
        return $goods;
    }

    public function getOneMinPrice($goods)
    {
        $goods = array($goods);
        $res = $this->getAllMinPrice($goods);
        return $res[0];
    }

    public function getMemberPrice($goods, $level)
    {
        global $_W, $_GPC;

        if(!empty($goods['isnodiscount'])){
            return;
        }

        $liveid = intval($_GPC['liveid']);
        if(!empty($liveid)){
            $isliving = p('live')->isLiving($liveid);
            if(!$isliving){
                $liveid = 0;
            }
        }

        // �жϻ�Ա�ȼ�״̬
        if(!empty($level['id'])){
            $level = pdo_fetch('select * from ' . tablename('mx_shop_member_level') . ' where id=:id and uniacid=:uniacid and enabled=1 limit 1', array(':id' =>$level['id'], ':uniacid' => $_W['uniacid']));
            $level = empty($level)? array(): $level;
        }

        $discounts = json_decode($goods['discounts'], true);
        if (is_array($discounts)) {
            $key = !empty($level['id']) ? 'level' . $level['id'] : 'default';
            if (!isset($discounts['type']) || empty($discounts['type'])) {

                $memberprice = $goods['minprice'];

                if (!empty($discounts[$key])){
                    $dd = floatval($discounts[$key]); //���õĻ�Ա�ۿ�
                    if ($dd > 0 && $dd < 10) {
                        $memberprice = round($dd / 10 * $goods['minprice'], 2);
                    }
                }else{
                    $dd = floatval($discounts[$key.'_pay']); //���õĻ�Ա�ۿ�

                    $md = floatval($level['discount']); //��Ա�ȼ��ۿ�
                    if (!empty($dd)){
                        $memberprice = round($dd, 2);
                    }else if ($md > 0 && $md < 10) {
                        $memberprice = round($md / 10 * $goods['minprice'], 2);
                    }
                }

                if($memberprice==$goods['minprice']){
                    return false;
                }

                return $memberprice;
            } else {
                //��ϸ�ۿ�
                $options = m('goods')->getOptions($goods);
                $marketprice =  array();
                foreach ($options as $option){
                    $discount = trim($discounts[$key]['option' . $option['id']]);
                    if($discount==''){
                        $discount = round(floatval($level['discount'])*10,2).'%';
                    }
                    if(!empty($liveid) && !empty($option['islive'])){
                        if($option['liveprice']>0 && $option['liveprice']<$option['marketprice']){
                            $option['marketprice'] = $option['liveprice'];
                        }
                    }
                    $optionprice = m('order')->getFormartDiscountPrice($discount, $option['marketprice']);
                    $marketprice[] =$optionprice;
                }
                $minprice = min($marketprice);
                $maxprice = max($marketprice);
                $memberprice = array(
                    'minprice'=>(float)$minprice,
                    'maxprice'=>(float)$maxprice
                );
                if($memberprice['maxprice']>$memberprice['minprice']){
                    $memberprice = $memberprice['minprice']."~".$memberprice['maxprice'];
                }else{
                    $memberprice = $memberprice['minprice'];
                }

                if($memberprice==$goods['minprice']){
                    return false;
                }

                return $memberprice;
            }
        }
        return;
    }

    public function getOptions($goods)
    {
        global $_W;
        $id = $goods['id'];
        $specs =false;
        $options = false;
        if (!empty($goods) && $goods['hasoption']) {
            $specs = pdo_fetchall('select* from ' . tablename('mx_shop_goods_spec') . ' where goodsid=:goodsid and uniacid=:uniacid order by displayorder asc', array(':goodsid' => $id, ':uniacid' => $_W['uniacid']));
            foreach($specs as &$spec) {
                $spec['items'] = pdo_fetchall('select * from '.tablename('mx_shop_goods_spec_item')." where specid=:specid order by displayorder asc",array(':specid'=>$spec['id']));
            }
            unset($spec);
            $options = pdo_fetchall('select * from ' . tablename('mx_shop_goods_option') . ' where goodsid=:goodsid and uniacid=:uniacid order by displayorder asc', array(':goodsid' => $id, ':uniacid' => $_W['uniacid']));
        }
        if($goods['ispresell']>0 && ($goods['preselltimeend'] == 0 || $goods['preselltimeend'] > time())){
            $options['marketprice'] = $options['presellprice'];
        }
        return $options;
    }

    /**
     * ��Ʒ����Ȩ��
     * @param array $goods
     * @param array $member
     * @return int
     */
    public function visit($goods=array(), $member=array()){
        global $_W;

        if(empty($goods)){
            return 1;
        }
        if(empty($member)){
            $member = m('member')->getMember($_W['openid']);
        }
        $showlevels = $goods['showlevels']!='' ? explode(',', $goods['showlevels']) : array();
        $showgroups = $goods['showgroups']!='' ? explode(',', $goods['showgroups']) : array();
        $showgoods = 0;
        if(!empty($member)){
            if((!empty($showlevels)&&in_array($member['level'], $showlevels)) || (!empty($showgroups) && in_array($member['groupid'], $showgroups)) || (empty($showlevels) && empty($showgroups))){
                $showgoods = 1;
            }
        }else{
            if(empty($showlevels) && empty($showgroups)){
                $showgoods = 1;
            }
        }
        return $showgoods;
    }

    /**
     *
     * �Ƿ��Ѿ����ظ��������Ʒ
     * @param $goods
     * @return bool
     */
    public function canBuyAgain($goods)
    {
        global $_W;
        $condition = '';
        $id = $goods['id'];
        if (isset($goods['goodsid'])){
            $id = $goods['goodsid'];
        }
        if (empty($goods['buyagain_islong'])){
            $condition = ' AND canbuyagain = 1';
        }
        $order_goods = pdo_fetchall("SELECT id,orderid FROM ".tablename('mx_shop_order_goods')." WHERE uniacid=:uniaicd AND openid=:openid AND goodsid=:goodsid {$condition}",array(':uniaicd'=>$_W['uniacid'],':openid'=>$_W['openid'],':goodsid'=>$id),'orderid');

        if (empty($order_goods)){
            return false;
        }
        $order = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('mx_shop_order')." WHERE uniacid=:uniaicd AND status>=:status AND id IN (".implode(',',array_keys($order_goods)).")",array(':uniaicd'=>$_W['uniacid'],':status'=>(empty($goods['buyagain_condition'])?'1':'3')));
        return !empty($order);
    }

    /**
     * ʹ�õ��ظ�����ı���
     * @param $goods
     */
    public function useBuyAgain($orderid)
    {
        global $_W;
        $order_goods = pdo_fetchall("SELECT id,goodsid FROM ".tablename('mx_shop_order_goods')." WHERE uniacid=:uniaicd AND openid=:openid AND canbuyagain = 1 AND orderid <> :orderid",array(':uniaicd'=>$_W['uniacid'],':openid'=>$_W['openid'],'orderid'=>$orderid),'goodsid');
        if (empty($order_goods)){
            return false;
        }
        pdo_query('UPDATE '.tablename('mx_shop_order_goods')." SET `canbuyagain`='0' WHERE uniacid=:uniacid AND goodsid IN (".implode(',',array_keys($order_goods)).")",array(':uniacid'=>$_W['uniacid']));
    }

    public function getTaskGoods($openid, $goodsid, $rank, $log_id = 0, $join_id = 0, $optionid = 0, $total = 0)
    {
        global $_W;

        $is_task_goods = 0;
        $is_task_goods_option = 0;

        if(!empty($join_id)) {
            $task_plugin = p('task');
            $flag = 1;
        } elseif(!empty($log_id)) {
            $task_plugin = p('lottery');
            $flag = 2;
        }

        $param = array();
        $param['openid'] = $openid;
        $param['goods_id'] = $goodsid;
        $param['rank'] = $rank;
        $param['join_id'] = $join_id;
        $param['log_id'] = $log_id;
        $param['goods_spec'] = $optionid;
        $param['goods_num'] = $total;

        if ($task_plugin && (!empty($join_id) || !empty($log_id))) {
            $task_goods = $task_plugin->getGoods($param);
        }

        if (!empty($task_goods) && empty($total) && (!empty($join_id) || !empty($log_id))) {
            if (!empty($task_goods['spec'])) {
                foreach ($task_goods['spec'] as $k => $v) {
                    if (empty($v['total'])) {
                        unset($task_goods['spec'][$k]);
                        continue;
                    }

                    if (!empty($optionid)) {
                        if ($k == $optionid) {
                            $task_goods['marketprice'] = $v['marketprice'];
                            $task_goods['total'] = $v['total'];
                        } else {
                            unset($task_goods['spec'][$k]);
                        }
                    }

                    if (!empty($optionid) && $k != $optionid) {
                        unset($task_goods['spec'][$k]);
                    } else if (!empty($optionid) && $k != $optionid) {
                        $task_goods['marketprice'] = $v['marketprice'];
                        $task_goods['total'] = $v['total'];
                    }
                }
                if (!empty($task_goods['spec'])) {
                    $is_task_goods = $flag;
                    $is_task_goods_option = 1;
                }
            } else {
                if (!empty($task_goods['total'])) {
                    $is_task_goods = $flag;
                }
            }
        }

        $data = array();
        $data['is_task_goods'] = $is_task_goods;
        $data['is_task_goods_option'] = $is_task_goods_option;
        $data['task_goods'] = $task_goods;

        return $data;
    }

    public function wholesaleprice($goods)
    {

        $goods2= array();

        foreach($goods as $good)
        {
            if($good['type']==4)
            {
                if(empty($goods2[$good['goodsid']]))
                {
                    $intervalprices = array();
                    if($good['intervalfloor']>0)
                    {
                        $intervalprices[]=array("intervalnum"=>intval($good['intervalnum1']),"intervalprice"=>floatval($good['intervalprice1']));
                    }
                    if($good['intervalfloor']>1)
                    {
                        $intervalprices[]=array("intervalnum"=>intval($good['intervalnum2']),"intervalprice"=>floatval($good['intervalprice2']));
                    }
                    if($good['intervalfloor']>2)
                    {
                        $intervalprices[]=array("intervalnum"=>intval($good['intervalnum3']),"intervalprice"=>floatval($good['intervalprice3']));
                    }

                    $goods2[$good['goodsid']]=array('goodsid'=>$good['goodsid'],'total'=>$good['total'],'intervalfloor'=>$good['intervalfloor'],'intervalprice'=>$intervalprices);
                }
                else
                {
                    $goods2[$good['goodsid']]['total']+=$good['total'];
                }
            }
        }

        foreach($goods2 as $good2) {
            $intervalprices2 = iunserializer($good2['intervalprice']);

            $price=0;
            foreach($intervalprices2 as $intervalprice)
            {
                if($intervalprice['intervalnum']<=$good2['total'])
                {
                    $price=$intervalprice['intervalprice'];
                }
            }

            foreach($goods as &$good)
            {
                if($good['goodsid']==$good2['goodsid'])
                {
                    $good['wholesaleprice']=$price;
                    $good['goodsalltotal']=$good2['total'];
                }
            }

            unset($good);
        }

        return $goods;

    }
    public function createcode($parameter) {
        global $_W;
        $path = IA_ROOT . "/addons/mx_shop/data/goodscode/" . $_W['uniacid'] . "/";
        $goodsid = $parameter['goodsid'];
        $qrcode = $parameter['qrcode'];
        $data = $parameter['codedata'];
        $mid = $parameter['mid'];
        $codeshare = $parameter['codeshare'];

        if (!is_dir($path)) {
            load()->func('file');
            mkdirs($path);
        }
        //�ļ����ƣ���������б䶯����������
        $md5 = md5(json_encode(array(
            'uniacid' => $_W['uniacid'],
            'goodsid' => $goodsid,
            'title' => $data['title']['text'],
            'price' => $data['price']['text'],
            'codeshare' => $parameter['codeshare'],
            'mid' => $mid
        )));
        $file = $md5 . '.jpg';
        if (!is_file($path . $file)) {
            //δ���ɹ������ά��仯
            //���ɱ���
            set_time_limit(0);
            @ini_set("memory_limit", "256M");
            if($codeshare==1){
                $target = imagecreatetruecolor(640, 1060);
                $color = imagecolorAllocate($target, 255, 255, 255);   //����һ����ɫ
                imagefill($target, 0, 0, $color);                 // �����Ͻǿ�ʼ���
                imagecopy($target, $target, 0, 0, 0, 0, 640, 1060);

                $target = $this->mergeText($target, $data['shopname'], $data['shopname']['text']);
                $thumb = preg_replace('/\/0$/i', '/96', $data['portrait']['thumb']);
                $target = $this->mergeImage($target, $data['portrait'], $thumb);
                $thumb = preg_replace('/\/0$/i', '/96', $data['thumb']['thumb']);
                $target = $this->mergeImage($target, $data['thumb'], $thumb);
                $qrcode = preg_replace('/\/0$/i', '/96', $data['qrcode']['thumb']);
                $target = $this->mergeImage($target, $data['qrcode'], $qrcode);
                $target = $this->mergeText($target, $data['title'], $data['title']['text']);
                $target = $this->mergeText($target, $data['price'], $data['price']['text']);
                $target = $this->mergeText($target, $data['desc'], $data['desc']['text']);

                imagepng($target, $path . $file);
                imagedestroy($target);

            }if($codeshare==2){
                $target = imagecreatetruecolor(640, 640);
                $color = imagecolorAllocate($target, 255, 255, 255);   //����һ����ɫ
                imagefill($target, 0, 0, $color);                 // �����Ͻǿ�ʼ���

                $colorline = imagecolorallocate($target,0,0,0);//����һ����ɫ���Թ�ʹ��
                imageline($target,0,190,640,190,$colorline);

                $red = imagecolorallocate($target,254, 155, 68);//����һ����ɫ���Թ�ʹ��
                //imagerectangle($target,0,560,640,640,$red);
                imagefilledrectangle($target,0,560,640,640,$red);

                imagecopy($target, $target, 0, 0, 0, 0, 640, 640);

                $thumb = preg_replace('/\/0$/i', '/96', $data['thumb']['thumb']);
                $target = $this->mergeImage($target, $data['thumb'], $thumb);
                $target = $this->mergeText($target, $data['title'], $data['title']['text']);
                $target = $this->mergeText($target, $data['price'], $data['price']['text']);
                $qrcode = preg_replace('/\/0$/i', '/96', $data['qrcode']['thumb']);
                $target = $this->mergeImage($target, $data['qrcode'], $qrcode);
                $target = $this->mergeText($target, $data['desc'], $data['desc']['text']);
                $target = $this->mergeText($target, $data['shopname'], $data['shopname']['text'],true);

                imagepng($target, $path . $file);
                imagedestroy($target);

            }elseif($codeshare==3) {
                $target = imagecreatetruecolor(640, 1060);
                $color = imagecolorAllocate($target, 245, 244, 249);   //����һ����ɫ
                imagefill($target, 0, 0, $color);                 // �����Ͻǿ�ʼ���
                imagecopy($target, $target, 0, 0, 0, 0, 640, 1008);

                $target = $this->mergeText($target, $data['title'], $data['title']['text']);
                $target = $this->mergeText($target, $data['price'], $data['price']['text']);
                $target = $this->mergeText($target, $data['desc'], $data['desc']['text']);

                $thumb = preg_replace('/\/0$/i', '/96', $data['thumb']['thumb']);
                $target = $this->mergeImage($target, $data['thumb'], $thumb);

                $qrcode = preg_replace('/\/0$/i', '/96', $data['qrcode']['thumb']);
                $target = $this->mergeImage($target, $data['qrcode'], $qrcode);
                //return $target;
                imagepng($target, $path . $file);
                imagedestroy($target);
            }

        }
        $img = $_W['siteroot'] . "addons/mx_shop/data/goodscode/" . $_W['uniacid'] . "/" . $file;

        return $img;
    }
    public function createImage($imgurl)
    {
        load()->func('communication');
        $resp = ihttp_request($imgurl);
        if ($resp['code'] == 200 && !empty($resp['content'])) {
            return imagecreatefromstring($resp['content']);
        }
        $i = 0;
        while ($i < 3) {
            $resp = ihttp_request($imgurl);
            if ($resp['code'] == 200 && !empty($resp['content'])) {
                return imagecreatefromstring($resp['content']);
            }
            $i++;
        }
        return "";
    }

    public function mergeImage($target, $data, $imgurl) {

        $img = $this->createImage($imgurl);
        $w = imagesx($img);
        $h = imagesy($img);
        imagecopyresized($target, $img, $data['left'], $data['top'], 0, 0, $data['width'], $data['height'], $w, $h);
        imagedestroy($img);
        return $target;
    }

    public function mergeText($target, $data, $text,$center=false) {
        $font = IA_ROOT . "/addons/mx_shop/static/fonts/msyh.ttf";
        $colors = $this->hex2rgb($data['color']);
        $color = imagecolorallocate($target, $colors['red'], $colors['green'], $colors['blue']);
        if($center) {
            $fontBox = imagettfbbox($data['size'], 0, $font, $data['text']);//����ˮƽ����ʵ��
            imagettftext($target, $data['size'], 0, ceil(($data['width'] - $fontBox[2]) / 2), $data['top'] + $data['size'], $color, $font, $text);
        }else{
            imagettftext($target, $data['size'], 0, $data['left'], $data['top'] + $data['size'], $color, $font, $text);
        }
        return $target;
    }
    function hex2rgb($colour) {
        if ($colour[0] == '#') {
            $colour = substr($colour, 1);
        }
        if (strlen($colour) == 6) {
            list( $r, $g, $b ) = array($colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5]);
        } elseif (strlen($colour) == 3) {
            list( $r, $g, $b ) = array($colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2]);
        } else {
            return false;
        }
        $r = hexdec($r);
        $g = hexdec($g);
        $b = hexdec($b);
        return array('red' => $r, 'green' => $g, 'blue' => $b);
    }

}
